package com.huifaapp.mall.uitils;

import android.os.Handler;
import android.os.Message;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.huifaapp.mall.APP;

import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;


/**
 * author: wangZ
 * date：2017/9/18 16:48
 * FileName：JPushUitil
 * description：j极光工具类
 */

public class JPushUitil {
    private SPUtils sp;
    public static final String KEY_STATE = "STATE";
    private static JPushUitil mJPushUitil = null;

    private JPushUitil() {
        sp = SPUtils.getInstance("JPushUitil");
    }

    public static JPushUitil getInstance() {
        if ((mJPushUitil == null)) {

            synchronized (JPushUitil.class) {
                if (mJPushUitil == null) {
                    mJPushUitil = new JPushUitil();
                }
            }
        }
        return mJPushUitil;
    }

    /**
     * 设置极光的Tag
     *
     * @param
     */
    public void setJPushTagState(String alias) {
        boolean b = sp.getBoolean(KEY_STATE, false);
        if (!sp.getBoolean(KEY_STATE, false)) {
            startPush();
            JPushInterface.setAliasAndTags(APP.getInstance(), alias, null, mTaCallback);
        }

    }

    /**
     * 获取极光的Tag设置状态
     *
     * @return true 已经设置过,false还没有设置过
     */
    public boolean getJPushTagState() {
        return sp.getBoolean(KEY_STATE, false);

    }

    /**
     * 获取推送状态
     *
     * @return
     */
    public boolean getJPushState() {
        return JPushInterface.isPushStopped(APP.getInstance());

    }

    /**
     * 停止推送
     */
    public void stopPush() {

        if (!JPushInterface.isPushStopped(APP.getInstance())) {
            JPushInterface.stopPush(APP.getInstance());
            JPushInterface.clearAllNotifications(APP.getInstance());//清除所有通知
        }

//        removePushTag();
    }

    /**
     * 移除推送
     */
    public void removePush() {
        stopPush();
        //清除别名
        sp.remove(KEY_STATE);
    }

    /**
     * 开始推送
     */
    public void startPush() {
        if (JPushInterface.isPushStopped(APP.getInstance())) {
            JPushInterface.resumePush((APP.getInstance()));
        }

//        if (getJPushTagState()) {
//            setJPushTagState(true);
//        }
    }

    //设置极光的回调
    public final TagAliasCallback mTaCallback = new TagAliasCallback() {
        @Override
        public void gotResult(int code, String alias, Set<String> tags) {
            String logs;
            switch (code) {
                case 0:
                    logs = "Set tag and alias success";

                    // 建议这里往 SharePreference 里写一个成功设置的状态。成功设置一次后，以后不必再次设置了。
                    sp.put(KEY_STATE, true);
                    LogUtils.e("TAG", logs);
                    if (JPushInterface.isPushStopped(APP.getInstance())) {
                        JPushInterface.resumePush((APP.getInstance()));
                    }

                    break;
                case 6002:
                    logs = "Failed to set alias and tags due to timeout. Try again after 60s.";
                    // 延迟 60 秒来调用 Handler 设置别名
                    LogUtils.e("TAG", logs);
                    mHandler.sendMessageDelayed(mHandler.obtainMessage(MESSAGE_SET_ALIAS, alias), 1000 * 10);
                    break;
                default:
                    logs = "Failed with errorCode = " + code;
                    LogUtils.e("TAG", logs);
            }
//            ToastUtil.showShort(MainActivity.this,logs);
        }
    };
    private static final int MESSAGE_SET_ALIAS = 0x01;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            String alias = (String) msg.obj;
            switch (what) {
                case MESSAGE_SET_ALIAS:
                    JPushInterface.setAliasAndTags(APP.getInstance(),
                            alias,
                            null,
                            mTaCallback);
                    break;
            }
        }
    };
}
